﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using FRAFV.Common;
using FRAFV.Common.Localization;
using System.Xml.Serialization;

namespace FRAFV.Geo.Universe
{
	public interface IPosition
	{
		double RightAscension { get; set; }
		double Declination { get; set; }
	}

	[XmlRoot(Namespace = "urn:universe-geo-schemas-xml-frafv:star")]
	public class Star: IPosition
	{
		public enum StarCatalog
		{
			BSC,
			HIP,
			Other
		}

		public struct StarCode: IKeyItem<StarCatalog, string>
		{
			public StarCatalog Catalog { get; private set; }
			public string Value { get; set; }
			StarCatalog IKeyItem<StarCatalog, string>.Key { get { return this.Catalog; } }

			public StarCode(StarCatalog catalog, string code)
				: this()
			{
				this.Catalog = catalog;
				this.Value = code;
			}

			public override string ToString()
			{
				return this.Value;
			}
		}

		public struct StarID: IKeyItem<StarCatalog, int>
		{
			public StarCatalog Catalog { get; private set; }
			public int Value { get; set; }
			StarCatalog IKeyItem<StarCatalog, int>.Key { get { return this.Catalog; } }

			public StarID(StarCatalog catalog, int id)
				: this()
			{
				this.Catalog = catalog;
				this.Value = id;
			}

			public override string ToString()
			{
				return this.Catalog == StarCatalog.Other ? this.Value.ToString() : String.Format("{0} {1}", this.Catalog, this.Value);
			}
		}

		[XmlAttribute]
		public double RightAscension { get; set; }
		[XmlAttribute]
		public double Declination { get; set; }
		[XmlAttribute]
		public double Magnitude { get; set; }
		[XmlIgnore]
		public Color Color { get; set; }
		[XmlAttribute]
		public string ColorHex
		{
			get { return this.Color.ToArgb().ToString("X6"); }
			set { this.Color = Color.FromArgb(int.Parse(value, NumberStyles.HexNumber)); }
		}
		[XmlAttribute]
		public double? BV { get; set; }
		[XmlArrayItem("ID")]
		public KeyCollection<StarCatalog, int, StarID> ID { get; private set; }
		[XmlArrayItem("Code")]
		public KeyCollection<StarCatalog, string, StarCode> Code { get; private set; }
		[XmlArrayItem("Name")]
		public LocalizedLabel Name { get; private set; }

		public Star()
		{
			this.Name = new LocalizedLabel();
			this.Code = new KeyCollection<StarCatalog, string, StarCode>();
			this.ID = new KeyCollection<StarCatalog, int, StarID>();
		}
	}
}
